package api

import (
	"context"
	"fmt"
	"myproject/backend/types"
	"myproject/backend/utils"
)

type ReportApi struct {
	Ctx context.Context
}

func NewReportApi() *ReportApi {
	return &ReportApi{}
}

func (a *ReportApi) ListTreeChecklistInfos(req types.ChecklistCodeReq) types.ReportCheckListResp {
	resp := types.ReportCheckListResp{}
	params := map[string]interface{}{
		"page": req.Page,
		"size": req.Size,
	}
	if req.Code != "" {
		params["code"] = req.Code
	}
	if req.StartDate != "" {
		params["startDate"] = req.StartDate
	}
	if req.EndDate != "" {
		params["endDate"] = req.EndDate
	}
	if req.Phone != "" {
		params["phone"] = req.Phone
	}
	if req.CustomerName != "" {
		params["customerName"] = req.CustomerName
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method:      "GET",
		Suffix:      "/report/checklist/listTreeChecklistInfos",
		Resp:        &resp,
		QueryParams: params,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

func (a *ReportApi) QueryChecklistInfoById(checkId int64) types.ChecklistDataForApiResp {
	resp := types.ChecklistDataForApiResp{}
	body := map[string]interface{}{
		"id": checkId,
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/report/checklist/QueryChecklistInfoById",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// QueryRegion 查询区域数据
func (a *ReportApi) QueryRegion(req types.QueryRegionReq) types.QueryRegionDataResp {
	resp := types.QueryRegionDataResp{}
	body := map[string]interface{}{
		"projectIds":  req.ProjectIds,
		"checklistId": req.ChecklistId,
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/report/region/QueryRegion",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 一键下发SendMessageToDevices
func (a *ReportApi) SendMessageToDevices(req types.SendMessageToDevicesReq) types.SendMessageToDevicesResp {
	resp := types.SendMessageToDevicesResp{}
	body := map[string]interface{}{
		"projectIdList": req.ProjectIdList,
		"scan":          req.Scan,
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/report/checklist/SendMessageToDevices",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 选择设备检查数据
func (a *ReportApi) ListCustomerResultInfos(req types.ResultListReq) types.ResultDataForApiResp {
	resp := types.ResultDataForApiResp{}
	queryParams := map[string]interface{}{
		"page": req.Page,
		"size": req.Size,
	}
	if req.Name != "" {
		queryParams["name"] = req.Name
	}
	if req.Phone != "" {
		queryParams["phone"] = req.Phone
	}
	if req.DeviceSn != "" {
		queryParams["deviceSn"] = req.DeviceSn
	}
	if req.ProjectId > 0 {
		queryParams["projectId"] = req.ProjectId
	}
	if req.OrganId > 0 {
		queryParams["organId"] = req.OrganId
	}
	if req.StartDate != "" {
		queryParams["startDate"] = req.StartDate
	}
	if req.EndDate != "" {
		queryParams["endDate"] = req.EndDate
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method:      "GET",
		Suffix:      "/report/result/listCustomerResultInfos",
		Resp:        &resp,
		QueryParams: queryParams,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 查询配镜建议相关所有数据点
func (a *ReportApi) QueryGlassesResultInfo(req types.IdsReq) types.SendMessageToDevicesResp {
	resp := types.SendMessageToDevicesResp{}
	body := map[string]interface{}{
		"ids": req.Ids,
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/report/project/QueryGlassesResultInfo",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 查询配镜结果建议
func (a *ReportApi) QueryGlassesInfo(req types.IdReq) types.GlassesInfoReqResp {
	resp := types.GlassesInfoReqResp{}
	body := map[string]interface{}{
		"id": req.Id,
	}
	if req.OrganId > 0 {
		body["organId"] = req.OrganId
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/report/checklist/QueryGlassesInfo",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 模版列表筛选
func (a *ReportApi) ListTemplateInfos(req types.TemplateListReq) types.TemplateListForApiResp {
	resp := types.TemplateListForApiResp{}
	queryParams := map[string]interface{}{
		"page": req.Page,
		"size": req.Size,
	}
	if req.TemplateName != "" {
		queryParams["templateName"] = req.TemplateName
	}
	if req.StartDate != "" {
		queryParams["startDate"] = req.StartDate
	}
	if req.EndDate != "" {
		queryParams["endDate"] = req.EndDate
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method:      "GET",
		Suffix:      "/report/template/ListTemplateInfos",
		Resp:        &resp,
		QueryParams: queryParams,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

func (a *ReportApi) CreateReportInfo(req types.ReportInfoForApiOne) types.ReportPdfForApiResp {
	resp := types.ReportPdfForApiResp{}
	body := make(map[string]interface{})
	if req.ID > 0 {
		body["id"] = req.ID
	}
	if req.CustomRelativeId > 0 {
		body["customRelativeId"] = req.CustomRelativeId
	}
	if req.ProjectName != "" {
		body["projectName"] = req.ProjectName
	}
	if req.TemplateId > 0 {
		body["templateId"] = req.TemplateId
	}
	if req.Sort > 0 {
		body["sort"] = req.Sort
	}
	if req.ResultData != "" {
		body["resultData"] = req.ResultData
	}
	if req.OrganId > 0 {
		body["organId"] = req.OrganId
	}
	if req.Name != "" {
		body["name"] = req.Name
	}
	if req.Phone != "" {
		body["phone"] = req.Phone
	}
	if req.Age > 0 {
		body["age"] = req.Age
	}
	if req.Sex > 0 {
		body["sex"] = req.Sex
	}
	if req.ListId > 0 {
		body["listId"] = req.ListId
	}
	if req.ResultInfo != "" {
		body["resultInfo"] = req.ResultInfo
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: fmt.Sprintf("/report/%s", req.TemplateApi),
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 报告管理
func (a *ReportApi) ListReportInfos(req types.CodeReq) types.ReportDataForApiListResp {
	resp := types.ReportDataForApiListResp{}
	query := make(map[string]interface{})
	if req.Code != "" {
		query["code"] = req.Code
	}
	if req.OrganId > 0 {
		query["organId"] = req.OrganId
	}
	if req.TemplateId > 0 {
		query["templateId"] = req.TemplateId
	}
	if req.StartDate != "" {
		query["startDate"] = req.StartDate
	}
	if req.EndDate != "" {
		query["endDate"] = req.EndDate
	}
	if req.Phone != "" {
		query["phone"] = req.Phone
	}
	if req.CustomerName != "" {
		query["customerName"] = req.CustomerName
	}
	if req.CustomerId > 0 {
		query["customerId"] = req.CustomerId
	}
	if req.RelationId > 0 {
		query["relationId"] = req.RelationId
	}
	if req.TimeType > 0 {
		query["timeType"] = req.TimeType
	}
	if req.Page > 0 {
		query["page"] = req.Page
	}
	if req.Size > 0 {
		query["size"] = req.Size
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method:      "GET",
		Suffix:      "/report/report/listReportInfos",
		QueryParams: query,
		Resp:        &resp,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
	}
	return resp
}

// 检查单列表筛选
func (a *ReportApi) ListChecklistInfos(req types.ChecklistCodeReq) types.ChecklistInfoForApiResp {
	resp := types.ChecklistInfoForApiResp{}
	params := map[string]interface{}{
		"page": req.Page,
		"size": req.Size,
	}
	if req.RelationId > 0 {
		params["relationId"] = req.RelationId
	}
	if req.Code != "" {
		params["code"] = req.Code
	}
	if req.StartDate != "" {
		params["startDate"] = req.StartDate
	}
	if req.EndDate != "" {
		params["endDate"] = req.EndDate
	}
	if req.Phone != "" {
		params["phone"] = req.Phone
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method:      "GET",
		Suffix:      "/report/checklist/listChecklistInfos",
		Resp:        &resp,
		QueryParams: params,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 列表展示组织项目
func (a *ReportApi) ListProjectInfos(req types.ListReq) types.ListDeviceRespResp {
	resp := types.ListDeviceRespResp{}
	queryParams := map[string]interface{}{
		"page": req.Page,
		"size": req.Size,
	}
	if req.Query != "" {
		queryParams["query"] = req.Query
	}
	if req.OrganId > 0 {
		queryParams["organId"] = req.OrganId
	}
	if req.IsCustomize > 0 {
		queryParams["isCustomize"] = req.IsCustomize
	}
	if req.IsDefmod > 0 {
		queryParams["isDefmod"] = req.IsDefmod
	}
	if req.ProjectName != "" {
		queryParams["projectName"] = req.ProjectName
	}
	if req.Status > 0 {
		queryParams["status"] = req.Status
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method:      "GET",
		Suffix:      "/report/project/listProjectInfos",
		Resp:        &resp,
		QueryParams: queryParams,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 添加检查单
func (a *ReportApi) CreateChecklistInfo(req types.ChecklistDataForApi) types.CreateChecklistResp {
	resp := types.CreateChecklistResp{}
	body := make(map[string]interface{})
	if req.ID > 0 {
		body["id"] = req.ID
	}
	if req.Code != "" {
		body["code"] = req.Code
	}
	if req.CustomerId > 0 {
		body["customerId"] = req.CustomerId
	}
	if req.CustomerName != "" {
		body["customerName"] = req.CustomerName
	}
	if req.CustomerPhone != "" {
		body["customerPhone"] = req.CustomerPhone
	}
	if req.Age > 0 {
		body["age"] = req.Age
	}
	if req.Sex > 0 {
		body["sex"] = req.Sex
	}
	if req.Birthday > 0 {
		body["birthday"] = req.Birthday
	}
	if req.RelationId > 0 {
		body["relationId"] = req.RelationId
	}
	if req.RelationName != "" {
		body["relationName"] = req.RelationName
	}
	if req.Phone != "" {
		body["phone"] = req.Phone
	}
	if req.BaseRelationId > 0 {
		body["baseRelationId"] = req.BaseRelationId
	}
	if req.BaseRelationName != "" {
		body["baseRelationName"] = req.BaseRelationName
	}
	if req.OrganId > 0 {
		body["organId"] = req.OrganId
	}
	if req.ReportPdf != "" {
		body["reportPdf"] = req.ReportPdf
	}
	if req.Status > 0 {
		body["status"] = req.Status
	}
	if req.ResultInfo != "" {
		body["resultInfo"] = req.ResultInfo
	}
	if req.Advice != "" {
		body["advice"] = req.Advice
	}
	if len(req.ProjectId) > 0 {
		body["projectId"] = req.ProjectId
	}
	if len(req.ProjectList) > 0 {
		body["projectList"] = req.ProjectList
	}
	if req.CompanyName != "" {
		body["companyName"] = req.CompanyName
	}
	if req.Origin != "" {
		body["origin"] = req.Origin
	}
	if req.Occupation != "" {
		body["occupation"] = req.Occupation
	}
	if req.WorkEnvironment != "" {
		body["workEnvironment"] = req.WorkEnvironment
	}
	if req.EyeDistance != "" {
		body["eyeDistance"] = req.EyeDistance
	}
	if req.Complaints != "" {
		body["complaints"] = req.Complaints
	}
	if req.MedicalCardNumber != "" {
		body["medicalCardNumber"] = req.MedicalCardNumber
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/report/checklist/createChecklistInfo",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
	}
	return resp
}

// 更换客户检查单检查结果
func (a *ReportApi) UpdateResultInfo(req types.UpdateResultReq) types.JsResp {
	resp := types.JsResp{}
	body := map[string]interface{}{
		"id":     req.Id,
		"listId": req.ListId,
	}
	if req.DeviceSn != "" {
		body["deviceSn"] = req.DeviceSn
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/report/result/UpdateResultInfo",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 编辑项目区域数据
func (a *ReportApi) UpdateRegionResult(req types.UpdateRegionResultReq) types.JsResp {
	resp := types.JsResp{}
	body := map[string]interface{}{}
	if req.ListId > 0 {
		body["listId"] = req.ListId
	}
	if req.ListCode != "" {
		body["listCode"] = req.ListCode
	}
	if req.CustomId > 0 {
		body["customId"] = req.CustomId
	}
	if req.CustomRelativeId > 0 {
		body["customRelativeId"] = req.CustomRelativeId
	}
	if req.BaseRelationId > 0 {
		body["baseRelationId"] = req.BaseRelationId
	}
	if req.OrganizetionId > 0 {
		body["organizetionId"] = req.OrganizetionId
	}
	if req.Name != "" {
		body["name"] = req.Name
	}
	if req.Phone != "" {
		body["phone"] = req.Phone
	}
	if req.Age > 0 {
		body["age"] = req.Age
	}
	if req.Sex > 0 {
		body["sex"] = req.Sex
	}
	if len(req.DataPayload) > 0 {
		body["dataPayload"] = req.DataPayload
	}
	if req.Scan != "" {
		body["scan"] = req.Scan
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/report/region/UpdateRegionResult",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// ai生成诊断结果
func (a *ReportApi) AiDiagnosisInfo(req types.AiContentReq) types.AiResponsePayloadListResp {
	resp := types.AiResponsePayloadListResp{}
	body := map[string]interface{}{
		"checklistId":  req.ChecklistId,
		"content":      req.Content,
		"organId":      req.OrganId,
		"questionType": req.QuestionType,
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/report/checklist/AiDiagnosisInfo",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 使用ai诊断结果建议
func (a *ReportApi) UpdateChecklistAiDiagnosisInfo(req types.UseAiGenerateContentReq) types.JsResp {
	resp := types.JsResp{}
	body := map[string]interface{}{
		"isUse":    req.IsUse,
		"uniqueId": req.UniqueId,
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/report/checklist/UpdateChecklistAiDiagnosisInfo",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// "为检查单添加左右眼诊断"
func (a *ReportApi) CreateChecklistDiagnosisInfo(req types.ChecklistDiagnosisListReq) types.JsResp {
	resp := types.JsResp{}
	body := map[string]interface{}{
		"checklistId": req.ChecklistId,
		"dataType":    req.DataType,
	}
	if len(req.List) > 0 {
		body["list"] = req.List
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/report/checklist/CreateChecklistDiagnosisInfo",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 添加配镜结果建议
func (a *ReportApi) CreateGlassesInfo(req types.GlassesInfoReq) types.JsResp {
	resp := types.JsResp{}
	body := map[string]interface{}{
		"id":            req.Id,
		"checklistId":   req.ChecklistId,
		"glassesResult": req.GlassesResult,
		"glassesAdvice": req.GlassesAdvice,
		"trialPlan":     req.TrialPlan,
		"other":         req.Other,
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/report/checklist/CreateGlassesInfo",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 检查数据点数据
//
//	get /ListResultData (ListReq) returns (ListResp)
func (a *ReportApi) ListReportResultData(req types.ReportResultDataReq) types.ReportResultResp {
	resp := types.ReportResultResp{}
	params := map[string]any{}
	params["page"] = req.Page
	params["size"] = req.Size
	if req.Query != "" {
		params["query"] = req.Query
	}
	if req.OrganId > 0 {
		params["organId"] = req.OrganId
	}
	if req.IsCustomize > 0 {
		params["isCustomize"] = req.IsCustomize
	}
	if req.IsDefmod > 0 {
		params["isDefmod"] = req.IsDefmod
	}
	if req.ProjectName != "" {
		params["projectName"] = req.ProjectName
	}
	if req.Status > 0 {
		params["status"] = req.Status
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method:      "GET",
		Suffix:      "/report/result/ListResultData",
		Resp:        &resp,
		QueryParams: params,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
	}
	return resp
}

// 查询检查单结果趋势
func (a *ReportApi) QueryResultTrendInfos(req types.TrendReq) types.ResultTrendInfoResp {
	resp := types.ResultTrendInfoResp{}
	body := map[string]any{}
	if req.Name != "" {
		body["name"] = req.Name
	}
	if req.Phone != "" {
		body["phone"] = req.Phone
	}
	if req.OrganId > 0 {
		body["organId"] = req.OrganId
	}
	if req.TimeType > 0 {
		body["timeType"] = req.TimeType
	}
	if len(req.ResultData) > 0 {
		body["resultData"] = req.ResultData
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/report/result/QueryResultTrendInfos",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
	}
	return resp
}

// 查询检查单结果趋势
func (a *ReportApi) ListResultInfos(req types.ResultListReq) types.ResultDataForApiResp {
	resp := types.ResultDataForApiResp{}
	queryParams := map[string]interface{}{
		"page": req.Page,
		"size": req.Size,
	}
	if req.Name != "" {
		queryParams["name"] = req.Name
	}
	if req.Phone != "" {
		queryParams["phone"] = req.Phone
	}
	if req.DeviceSn != "" {
		queryParams["deviceSn"] = req.DeviceSn
	}
	if req.ProjectId > 0 {
		queryParams["projectId"] = req.ProjectId
	}
	if req.OrganId > 0 {
		queryParams["organId"] = req.OrganId
	}
	if req.StartDate != "" {
		queryParams["startDate"] = req.StartDate
	}
	if req.EndDate != "" {
		queryParams["endDate"] = req.EndDate
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method:      "GET",
		Suffix:      "/report/result/listResultInfos",
		Resp:        &resp,
		QueryParams: queryParams,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
	}
	return resp
}

// 删除检查单
func (a *ReportApi) DeleteChecklistInfo(req types.DeleteChecklistReq) types.JsResp {
	resp := types.JsResp{}
	body := map[string]interface{}{
		"id": req.Id,
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/report/checklist/deleteChecklistInfoById",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}
